const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid:'',
    name:'',
    address:'',
    phone:'',
    phoneMsg:'手机号格式错误',
    phoneIsYes:false
  },

  setPhone(e){
    var that = this;
    this.setData({
      phone:e.detail
    },()=>{
      const reg = /^[1][3-9]\d{9}$/;
      if (reg.test(this.data.phone)) {
        that.setData({
          phoneIsYes:true,
          phoneMsg:''
        })
      }else{
        that.setData({
          phoneIsYes:false,
          phoneMsg:'手机号格式错误'
        })
      }
    })
  },
  setAddress(e){
    this.setData({
      address:e.detail
    })
  },
  setName(e){
    this.setData({
      name:e.detail
    })
  },
  save(){
    var that = this;
    if (!this.data.phoneIsYes || this.data.name.length == 0 || this.data.address.length == 0) {
      wx.showToast({
        title: '请完善信息',
        icon:'none',
        duration:800
      })
      return;
    }else{
      wx.request({
        url: httpUrl + '/address',
        method:'PUT',
        header:{
          'Shop_token':wx.getStorageSync('Shop_token')
        },
        data:{
          uid:that.data.uid,
          name:that.data.name,
          phone:that.data.phone,
          address:that.data.address
        },
        success(res){
          if (res.data.code == 20031) {
            wx.showToast({
              title: '修改成功',
              icon:'none',
              duration:800
            });
            wx.navigateBack({
              delta: 1, // 返回的页面数，如果 delta 大于现有页面数，则返回到首页
              success: function (res) {
                console.log(res);
              },
              fail: function (err) {
                console.error(err);
              }
            })
          }
          else if (res.data.code == 401) {
            console.log(res.data.msg);
            wx.removeStorageSync('Shop_token');
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
          else if (res.data.code == 402) {
            console.log(res.data.msg);
            // wx.removeStorageSync('Shop_token');
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
          else{
            console.log(res.data.msg);
          }
        },
        fail(err){
          console.log(err);
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    this.setData({
      name:options.name,
      address:options.address,
      phone:options.phone,
      uid:options.uid
    },()=>{
      const reg = /^[1][3-9]\d{9}$/;
      if (reg.test(this.data.phone)) {
        that.setData({
          phoneIsYes:true,
          phoneMsg:''
        })
      }else{
        that.setData({
          phoneIsYes:false,
          phoneMsg:'手机号格式错误'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})